import storage from "@/utils/storage.js";
import config from '@/config/config';
import store from './store'
import uitls from './common/js/common.js'
// #ifndef VUE3
import Vue from 'vue'
import App from './App'

Vue.prototype.$to = function(url, animationType = 'pop-in', animationDuration = 300) {
	let array = ['/pages/index/index', '/pages/user/center/center'
	];
	//console.log("array.indexOf(url)",array.indexOf(url));


	// #ifdef MP-WEIXIN
	// 获取当前页面实例
	var pageInstance = getCurrentPages()[getCurrentPages().length - 1];
	// 获取当前页面中的所有 webview 组件
	var webviews = pageInstance.selectAllComponents('web-view');

	if (webviews.length >= 10) {
		// 如果当前页面中的 webview 数量已经超过 10 个，可以考虑关闭一些 webview 或者跳转到其他页面
		// 这里的示例是关闭当前页面中的第一个 webview
		for (let i = 0; i < 4; i++) {
			webviews[i].triggerEvent('message', {
				type: 'close'
			});
		}
	}

	// #endif



	if (array.indexOf(url) >= 0) {
		uni.switchTab({
			url
		})
	} else {
		uni.navigateTo({
			url,
			animationType,
			animationDuration,
			success: function(res) {
				console.log(res)
			},
			fail: function(e) {
				console.log(e)
			}
		})
	}

}

Vue.prototype.$navigateCheck = function(url, needHasLogin = false,needHasBoundHouse=false) {
	// uni.navigateTo({
	// 	url: `${url}`
	// })
	// return ;
	let hasLogin = storage.getHasLogin();
	let hasBoundHouse = storage.getBouseHouse();
	if(needHasLogin==false){
		uni.navigateTo({
			url: `${url}`
		})
		return ;
	}else if(needHasLogin==true){
		if (hasLogin && hasLogin != "") {
			if(needHasBoundHouse==true){
				if (hasBoundHouse==true){
					uni.navigateTo({
						url: `${url}`
					})
				}else{
					uni.showToast({
						title:"请先绑定房屋",
						duration:3000,
						icon:"none"
					});
					return ;
					uni.navigateTo({
						url: `/pages/user/my-house/my-house`
					})
					
				}
			}else{
				uni.navigateTo({
					url: `${url}`
				})
			}
			
			
		} else {
			console.log("yangzl");
			// #ifdef MP-WEIXIN
			uni.navigateTo({
				url: "/pages/login/wechatMPLogin",
			});
			// #endif
			// #ifndef MP-WEIXIN
			uni.navigateTo({
				url: "/pages/login/aliMPLogin",
			});
			//  #endif
		}
	}
	
}

Vue.config.productionTip = false
Vue.prototype.$store = store
Vue.prototype.$uitls = uitls
App.mpType = 'app'

const app = new Vue({
	store,
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import App from './App.vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif